package com.whr.utils;

import com.whr.bean.Node;
import com.whr.bean.Resources;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CompareResources {
    private List<Resources> totalList = new ArrayList<>();
    private List<Resources> allList;
    List<Node> nodes = new ArrayList<>();

    public List<Resources> sortRes(List<Resources> resourcesList) {
        allList = resourcesList;
        List<Resources> list0 = new ArrayList<>();
        for (Resources res : resourcesList) {
            if (res.getRparent() == null || res.getRparent().equals("")) {
                list0.add(res);
            }
        }
        Collections.sort(list0);

        compareFun(list0);
        return totalList;
    }

    public List<String> sortMenu(List<Resources> resourcesList) {
        List<String> totalMenu = new ArrayList<>();
        List<Resources> menus0 = sortRes(resourcesList);
        for (Resources res : menus0) {
            if (res.getRtype().equals("菜单")) {
                totalMenu.add(res.getRname());
            }
        }
        return totalMenu;
    }

    public List<Node> sortNode(List<Resources> resourcesList) {
        allList = sortRes(resourcesList);
        List<Resources> reses0 = new ArrayList<>();
        List<Node> tempNodes = new ArrayList<>();
        int i = 1;
        for (Resources res : allList) {
            if (res.getRparent() == null || res.getRparent().equals("")) {
                Node node = new Node();
                node.setId(i);
                node.setpId(0);
                node.setName(res.getRname());
                node.setPath(res.getRpath());
                nodes.add(node);
                tempNodes.add(node);
                reses0.add(res);
                i++;
            }
        }
        saveNode(reses0, tempNodes);
        return nodes;
    }

    private void saveNode(List<Resources> resList, List<Node> tempNodes) {
        List<Resources> resTemp = new ArrayList<>();
        List<Node> tempNodes1 = new ArrayList<>();
        int n = 0;
        for (int i = 0; i < resList.size(); i++) {
            n = 0;
            for (Resources res : allList) {
                if (res.getRparent().equals(resList.get(i).getRname())) {
                    Node node = new Node();
                    String id = tempNodes.get(i).getId() + "" + (n + 1);
                    node.setId(Integer.parseInt(id));
                    node.setpId(tempNodes.get(i).getId());
                    node.setName(res.getRname());
                    node.setPath(res.getRpath());
                    nodes.add(node);
                    tempNodes1.add(node);
                    resTemp.add(res);
                    n++;
                }
            }
        }
        if (!resTemp.isEmpty()) {
            saveNode(resTemp, tempNodes1);
        }
    }

    private void compareFun(List<Resources> resourcesList) {

        for (Resources res : resourcesList) {
            totalList.add(res);
            String rname = res.getRname();

            List<Resources> listch = new ArrayList<>();
            for (Resources res1 : allList) {
                if (res1.getRparent() == null || res1.getRparent().equals("")) {
                    continue;
                }
                if (res1.getRparent().equals(rname)) {
                    listch.add(res1);
                }
            }
            if (listch.isEmpty()) {
                continue;
            }
            Collections.sort(listch);
            compareFun(listch);
        }
    }
}